$(function(){
	
	var rank = $("#rank").val();
	var columns;
	if(rank == 0){
		columns = [
		           { "data": "customerName" },
		           { "data": "mobile" },
		           {
		   			render : function(data,type, row, meta) {
		                   return row["integration"] * 1 + row["usedIntegration"] * 1;
		              }
		   		 },
		           { "data": "usedIntegration" },
		           { "data": "integration" },
		           {
		               render : function(data,type, row, meta) {
		                   return $("#btnGroup").html();
		               }
		           }
		       ];
	}else if(rank == 1){
		columns = [
		           { "data": "customerName" },
		           { "data": "mobile" },
		           {
		   			render : function(data,type, row, meta) {
		                   return row["integration"] * 1 + row["usedIntegration"] * 1;
		              }
		   		 },
		           { "data": "usedIntegration" },
		           { "data": "integration" }
		       ];
	}

	$('.datatable-basic').DataTable({
		 "ajax" : {
			"url" : contextpath + "customer/integrationList",
			"data" : function(d) {
	            var query = $("#query").serializeObject();
	            query["size"] = d.length;
	            query["page"] = (d.start/d.length);
				return query;
			},
			"dataFilter": function(data){
	            var json = jQuery.parseJSON( data );
	            json.recordsTotal = json.totalElements;
	            json.recordsFiltered = json.totalElements;
	            json.data = json.content;
	 
	            return JSON.stringify(json);
	        }
		 },
		 "columns": columns,
	     "drawCallback": function( settings ) {
	    	 
	    	 $(".btn-integration-detail").click(function(){
	    		 var row = $('.datatable-basic').DataTable().row($(this).closest("tr")).data();
	    		 window.location.href = contextpath + "customer/integrationDetail/" + row.customerId;
	    	 });
	    	 
	    	 $(".btn-integration").click(function(){
				 var row = $('.datatable-basic').DataTable().row($(this).closest("tr")).data();
				 $(":input[name='integration']").val('');
				 $(":input[name='customerId']").val(row.customerId);
			 });
			 
			 $(".btn-integration-amount").click(function(){
				 var row = $('.datatable-basic').DataTable().row($(this).closest("tr")).data();
				 $(":input[name='amount']").val('');
				 $(":input[name='customerId']").val(row.customerId);
			 });
			 
	      }
	});

	
	$("#submitIntegration").click(function(){
		if($("#integrationForm").valid()){
			$.post(contextpath + "customer/adjustIntegration", $("#integrationForm").serialize(), function(){
				$('.datatable-basic').DataTable().draw();
				$('.close').click();
				swal("积分操作成功","", "success");
			})
		}
	});
	
	$("#submitIntegrationAmount").click(function(){
		if($("#integrationAmountForm").valid()){
			$.post(contextpath + "customer/adjustIntegration", $("#integrationAmountForm").serialize(), function(){
				$('.datatable-basic').DataTable().draw();
				$('.close').click();
				swal("编辑客户积分成功","", "success");
			})
		}
	});
	 
	$("#submit").click(function(){
		$('.datatable-basic').DataTable().draw();
	});
})
